/**
 * 应用管理管理初始化
 */
var Application = {
    id: "ApplicationTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Application.initColumn = function () {
    let res = [
            {title: '商户', field: 'userName', width: 105},
            {title: '应用名称', field: 'appName', width: 150},
            {title: '应用别名', field: 'appAlias', width: 150},
            {title: '下载地址', field: 'appUrl', width: 250},
            {title: '版本', field: 'appVersion', width: 80},
            {title: '推送支持', field: 'supportPush', width: 100,templet: function (data) {
                return data.supportPush === 1 ?'是':'否';
            }},
            {title: '包大小', field: 'packageSize', width: 90,templet: function (data) {
                    return data.packageSize + 'M';
                }},
            {title: '应用描述', field: 'appDesc', minWidth: 150},
            {title: '发布时间', field: 'createTime', width: 120 ,templet: (data) => {
                console.log(data.createTime);
                    if(data.createTime) {
                        return new Date(data.createTime).format("yyyy-MM-dd HH:mm:ss")
                    } else {
                        return ""
                    }
                }
            }
    ];
    if(!Application.isAdmin) {
        res.push({align: 'center', minWidth: 100,toolbar: '#onOffOperation'})
        res = res.filter((e) => e.field !== 'userName');
    } else {
        res.push({align: 'center', minWidth: 140,toolbar: '#operation'})
    }

    return res;
};

/**
 * 检查是否选中
 */
Application.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Application.seItem = selected[0];
        return true;
    }
};

Application.openUpdateApp = function(data) {
        var index = layer.open({
            type: 2,
            title: '更新应用',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/application/application_update/' + data.appId
        });
        this.layerIndex = index;
}

/**
 *  上架下架
 * */
Application.openOnOff = function(data) {
    let altTitle = data.status === 1 ? "下架": "上架"
    let confirmIndex = layer.confirm("确定"+altTitle+"应用?", {
        btn: ["确认"+altTitle, "取消"+altTitle]
    }, function () {
        layer.close(confirmIndex);
        onOffApplication(data);
    }, function () {
        layer.msg("取消" + altTitle, {icon: 1})
    })
}

function onOffApplication(data) {
    console.log(data);
    var ajax = new $ax(Feng.ctxPath + "/application/onoff", function (data) {
        Feng.success("更新成功!");
        Application.refresh();
    }, function (data) {
        Feng.error("更新失败!" + data.responseJSON.message + "!");
    });
    ajax.set("applicationId",data.appId);
    ajax.start();
}

/**
 * 点击添加应用管理
 */
Application.openAddApplication = function () {
    var index = layer.open({
        type: 2,
        title: '添加应用',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/application/application_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看应用管理详情
 */
Application.openApplicationDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '应用详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/application/application_update/' + Application.seItem.appId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除应用管理
 */
Application.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/application/delete", function (data) {
            Feng.success("删除成功!");
            Application.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("applicationId",this.seItem.appId);
        ajax.start();
    }
};

/**
 * 查询应用管理列表
 */
Application.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Application.table.refresh({query: queryData});
};

Application.refresh = () => {
    Application.table.reload("tableReload", null);
}

$(function () {
    Application.isAdmin = $("#isAdmin").val() === '超级管理员';
    let defaultColumns = Application.initColumn();
    layui.use(['table'], () => {
        let table = layui.table;
        Application.table = table;
        table.render({
            elem: '#app-list',
            height: 800,
            defaultToolbar: [],
            url: Feng.ctxPath + '/application/list', //数据接口
            title: '应用管理',
            toolbar: Application.isAdmin ? false:'#applist-toolbar', //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            cols: [defaultColumns],
            parseData: function (res) {
                console.log(res);
                let count = res ? res.length : 0
                return {
                    "code": 0,
                    "msg": "获取成功",
                    count,
                    "data": res
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        })
        table.on('toolbar(app-info)', (data) => {
            Application.openAddApplication();
        })
        table.on('tool(app-info)', (data) => {
            console.log(data);
            if(data.event === 'edit') {
                Application.openUpdateApp(data.data)
            } else{
                Application.openOnOff(data.data)
            }
        })
    })
    DateUtils.expandDate();
});
